﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml.Linq;
using Hal.CookieGetterSharp;



namespace NiconicoAPI {
    public class Account {
        private const string Url_Login = "https://secure.nicovideo.jp/secure/login?site=niconico";
        private const string Url_Crt = "http://live.nicovideo.jp/api/getalertstatus?";
        private const string Url_Nicovideo = "http://nicovideo.jp/";
        private const string Url_NicoLive = "http://live.nicovideo.jp/";

        //private const string Url_Login = "https://secure.nicovideo.jp/secure/login";

        internal CookieContainer NiconicoCokie { get; private set; }
        public bool IsLogin { get; private set; }

        public Account(BrowserType browser) {
            var cookieGetter = CookieGetter.CreateInstance(browser);
            var uri = new Uri(Url_NicoLive);
            var cookie = cookieGetter.GetCookie(uri, "user_session");
            NiconicoCokie = new CookieContainer();
            NiconicoCokie.Add(cookie);
        }


        public Account(string mail, string password) {
            IsLogin = Login(mail, password);
        }

        private bool Login(string mail, string password) {
            NiconicoCokie = new CookieContainer();
            var value = string.Format("mail={0}&password={1}", mail, password);
            var result = WebLogic.HttpPost(Url_Login, value, NiconicoCokie);
            return result.IndexOf("エラー") == -1;
        }
    }
}
